<script setup lang="ts">
import moment from "moment";
// import { ElMessageBox, ElNotification } from "element-plus";
import Increase from "@/assets/svg/streamline--graph-arrow-increase-solid.svg?component";
import Decrease from "@/assets/svg/streamline--graph-arrow-decrease-solid.svg?component";
import { RefreshLeft, Search, Grid } from "@element-plus/icons-vue";
import { computed, onMounted, reactive, ref } from "vue";
import {
  dayGenData,
  userData
} from "@/views/marketForecast/loadForecasting/testData";
// import { timeSlot } from "@/utils/index";
import VChart from "@/views/monitor/server/components/VChart.vue";
const loading = ref(false);
const allUser = reactive({
  data: userData
});
const users = computed(() => {
  if (queryForm.data.activeVppId) {
    let parent = userData.find(item => item.id === queryForm.data.activeVppId);
    if (parent.children) {
      return parent.children;
    } else {
      return [];
    }
  } else {
    return [];
  }
});
const terms = computed(() => {
  if (queryForm.data.activeUserId) {
    let parent = users.value.find(
      item => item.id === queryForm.data.activeUserId
    );
    if (parent.children) {
      return parent.children;
    } else {
      return [];
    }
  } else {
    return [];
  }
});
const vppChange = () => {
  queryForm.data.activeUserId = undefined;
  queryForm.data.activeTermId = undefined;
};
const userChange = () => {
  queryForm.data.activeTermId = undefined;
  queryForm.data.activeSbId = undefined;
};
const queryForm = reactive({
  data: {
    activeVppId: undefined,
    activeUserId: undefined,
    len: 1,
    activeTime: undefined,
    type: 1
  }
});
const types = reactive({
  data: [
    {
      value: 1,
      label: "负荷预测"
    }
  ]
});
const lenData = reactive({
  data: [
    {
      value: 1,
      label: "中长期"
    },
    {
      value: 2,
      label: "短期"
    },
    {
      value: 3,
      label: "超短期"
    }
  ]
});
const search = () => {
  if (queryForm.data.activeTime) {
    let data = dayGenData(
      queryForm.data.activeTime + "-01",
      queryForm.data.activeTime +
        "-" +
        moment(queryForm.data.activeTime).daysInMonth()
    );
    options.value.xAxis.data = data.map(item => item.time);
    options.value.series[0].data = data.map(item => ({ value: item.value1 }));
    tableData.data = data;
  }
};
const resetQuery = () => {
  queryForm.data = {
    activeVppId: undefined,
    activeUserId: undefined,
    len: 1,
    activeTime: undefined,
    type: 1
  };
  tableData.data = [];
};
// const pageObj = reactive({
//   data: {
//     currentPage: 1,
//     pageSize: 10,
//     total: 0
//   }
// });
// const handleSizeChange = () => {};
// const handleCurrentChange = async () => {};
const tableData = reactive({
  data: []
});
// const times = ref(timeSlot(15));
// const keys = ref(
//   Array(96)
//     .fill(1)
//     .map((_, index) => index + 1)
//     .map(i => `p${i > 9 ? i : "0" + i}`)
// );
// const dataFormat = data => {
//   let days = ["2024-06-15"];
//   let allData = [];
//   let xData = [];
//   days.forEach(i => {
//     let item = {};
//     item.dataDate = i + " 00:00:00";
//     keys.value.forEach(key => {
//       item[key] = null;
//     });
//     item.dataType = 1;
//     allData.push(item);
//     let item2 = {};
//     item2.dataDate = i + " 00:00:00";
//     keys.value.forEach(key => {
//       item2[key] = null;
//     });
//     item2.dataType = 2;
//     allData.push(item2);
//   });
//   for (let i = 0; i < allData.length; i++) {
//     for (let j = 0; j < data.length; j++) {
//       if (
//         allData[i].dataDate === data[j].dataDate &&
//         allData[i].dataType === data[j].dataType
//       ) {
//         allData[i] = Object.assign({}, allData[i], data[j]);
//       }
//     }
//   }
//   const ret = {};
//   allData.forEach(i => {
//     ret[i.dataDate] = {
//       "1": {},
//       "2": {}
//     };
//   });
//   allData.forEach(i => {
//     ret[i.dataDate][i.dataType + ""] = i;
//   });
//
//   let oneData = allData.filter(i => i.dataType === 1);
//   let twoData = allData.filter(i => i.dataType === 2);
//   let isd = [];
//   let tsd = [];
//   oneData.forEach(i => {
//     times.value.forEach((z, index) => {
//       xData.push(i.dataDate.split(" ")[0] + " " + z + ":00");
//       isd.push({
//         value: i[keys.value[index]]
//       });
//     });
//   });
//   twoData.forEach(i => {
//     times.value.forEach((z, index) => {
//       tsd.push({
//         value: i[keys.value[index]]
//       });
//     });
//   });
//   let start = (1 - 96 / xData.length) * 100;
//   options.value.xAxis.data = xData;
//   options.value.dataZoom[0].start = start;
//   options.value.dataZoom[1].start = start;
//   options.value.dataZoom[0].end = 100;
//   options.value.dataZoom[1].end = 100;
//
//   options.value.series[0].data = isd;
//   options.value.series[1].data = tsd;
// };
//
// const edit = row => {
//   addDialog.value = true;
//   dataFormat(genData("2024-06-15", "2024-06-15"));
// };
// const del = row => {
//   ElMessageBox.confirm("确认将该数据还原?", "系统提示", {
//     confirmButtonText: "确定",
//     cancelButtonText: "取消",
//     type: "warning"
//   }).then(() => {
//     ElNotification({
//       title: "系统提示",
//       message: "还原成功",
//       type: "success"
//     });
//   });
// };
// const addDialog = ref(false);
// const title = ref("数据对比");
const options = ref({
  dataZoom: [
    {
      show: true,
      type: "inside",
      start: 0,
      end: 100
    },
    {
      show: true,
      type: "slider",
      start: 0,
      end: 100
    }
  ],
  tooltip: {
    show: true,
    trigger: "axis"
  },
  legend: {
    show: true
  },
  xAxis: {
    type: "category",
    boundaryGap: true,
    axisLabel: {
      //x轴文字的配置
      show: true
      // interval: 0,//使x轴文字显示全
    },
    data: []
  },
  yAxis: {
    type: "value",
    min: "dataMin"
  },
  series: [
    {
      name: "日准确率",
      type: "bar",
      data: [],
      barMaxWidth: 40
    }
  ]
});
const init = () => {
  tableData.data = [];
};
const checkAll = ref(true);
const isIndeterminate = ref(false);
const checkedCities = ref([]);
const defaultColums = reactive({
  data: [
    {
      id: 1,
      label: "日期",
      key: "time",
      prop: "time",
      width: "",
      align: "center"
    },
    {
      id: 2,
      label: "全天准确率",
      key: "value1",
      prop: "value1",
      width: "",
      align: "center"
    },
    {
      id: 3,
      label: "预测尖峰值",
      key: "value2",
      prop: "value2",
      width: "",
      align: "center"
    },
    {
      id: 4,
      label: "预测低谷值",
      key: "value3",
      prop: "value3",
      width: "",
      align: "center"
    },
    {
      id: 5,
      label: "历史尖峰值",
      key: "value4",
      prop: "value4",
      width: "",
      align: "center"
    },
    {
      id: 6,
      label: "历史低谷值",
      key: "value5",
      prop: "value5",
      width: "",
      align: "center"
    },
    {
      id: 7,
      label: "尖峰准确率",
      key: "value6",
      prop: "value6",
      width: "",
      align: "center"
    },
    {
      id: 8,
      label: "低谷准确率",
      key: "value6",
      prop: "value6",
      width: "",
      align: "center"
    }
  ]
});
const upordown = type => {
  if (tableData.data.length === 0) return;
  let data = JSON.parse(JSON.stringify(tableData.data));
  data.sort((a, b) => {
    if (type === 1) {
      return a.value1 - b.value1;
    } else {
      return b.value1 - a.value1;
    }
  });
  options.value.series[0].data = data.map(item => ({ value: item.value1 }));
  options.value.xAxis.data = data.map(item => ({ value: item.time }));
};
const colums = reactive({
  data: [
    {
      id: 1,
      label: "日期",
      key: "time",
      prop: "time",
      width: "",
      align: "center"
    },
    {
      id: 2,
      label: "全天准确率",
      key: "value1",
      prop: "value1",
      width: "",
      align: "center"
    },
    {
      id: 3,
      label: "预测尖峰值",
      key: "value2",
      prop: "value2",
      width: "",
      align: "center"
    },
    {
      id: 4,
      label: "预测低谷值",
      key: "value3",
      prop: "value3",
      width: "",
      align: "center"
    },
    {
      id: 5,
      label: "历史尖峰值",
      key: "value4",
      prop: "value4",
      width: "",
      align: "center"
    },
    {
      id: 6,
      label: "历史低谷值",
      key: "value5",
      prop: "value5",
      width: "",
      align: "center"
    },
    {
      id: 7,
      label: "尖峰准确率",
      key: "value6",
      prop: "value6",
      width: "",
      align: "center"
    },
    {
      id: 8,
      label: "低谷准确率",
      key: "value6",
      prop: "value6",
      width: "",
      align: "center"
    }
  ]
});
onMounted(() => {
  init();
  checkedCities.value = colums.data.map(item => item.prop);
});
const queryShow = ref(true);
const getList = () => {
  search();
};

const handleCheckAllChange = (val: boolean) => {
  checkedCities.value = val ? defaultColums.data.map(item => item.prop) : [];
  isIndeterminate.value = false;
  if (checkedCities.value.length) {
    colums.data = defaultColums.data;
  } else {
    colums.data = [];
  }
};
const handleCheckedCitiesChange = (value: string[]) => {
  const checkedCount = value.length;
  checkAll.value = checkedCount === defaultColums.data.length;
  isIndeterminate.value =
    checkedCount > 0 && checkedCount < defaultColums.data.length;
  let cols = [];
  defaultColums.data.forEach(i => {
    if (value.indexOf(i.prop) > -1) {
      cols.push(i);
    }
  });
  colums.data = cols;
};
</script>

<template>
  <div class="main h-[calc(100%-29px)]">
    <el-card v-loading="loading" class="w-full my-card-full">
      <el-row v-if="queryShow">
        <el-form :inline="true" :model="queryForm.data" label-width="0px">
          <el-form-item>
            <el-select v-model="queryForm.data.type" style="width: 100px">
              <el-option
                v-for="item in types.data"
                :key="item.value"
                :value="item.value"
                :label="item.label"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="queryForm.data.activeVppId"
              placeholder="选择场站"
              style="width: 120px"
              @change="vppChange"
            >
              <el-option
                v-for="item in allUser.data"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="queryForm.data.activeUserId"
              placeholder="选择用户"
              style="width: 120px"
              @change="userChange"
            >
              <el-option
                v-for="item in users"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="queryForm.data.activeTime"
              unlink-panels
              type="month"
              placeholder="请选择月份"
              value-format="YYYY-MM"
              date-format="YYYY-MM"
            />
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="queryForm.data.len"
              placeholder="选择类型"
              style="width: 90px"
              @change="userChange"
            >
              <el-option
                v-for="item in lenData.data"
                :key="item.value"
                :value="item.value"
                :label="item.label"
              />
            </el-select>
          </el-form-item>
          <el-form-item class="ml-[10px]">
            <el-button type="primary" :icon="Search" @click="search"
              >查询</el-button
            >
            <el-button type="primary" :icon="Search" @click="search"
              >计算</el-button
            >
            <el-button :icon="RefreshLeft" @click="resetQuery">重置</el-button>
            <el-button type="primary">导出</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <div class="w-full h-[400px]">
        <v-chart :options="options" />
      </div>
      <div class="w-full flex justify-between mb-[10px]">
        <div class="buttons flex align-center">
          <el-button type="primary" :icon="Increase" @click="upordown(1)" />
          <el-button type="primary" :icon="Decrease" @click="upordown(2)" />
          <!--          <el-button-->
          <!--            type="success"-->
          <!--            :icon="Edit"-->
          <!--            :disabled="!(multipleSelection.length === 1)"-->
          <!--            @click="editSelect"-->
          <!--          >修改</el-button-->
          <!--          >-->
          <!--          <el-button-->
          <!--            type="danger"-->
          <!--            :icon="Delete"-->
          <!--            :disabled="!multipleSelection.length"-->
          <!--            @click="delSelect"-->
          <!--          >删除</el-button-->
          <!--          >-->
          <!--            <el-button type="warning" :icon="Download" @click="exportData"-->
          <!--              >导出</el-button-->
          <!--            >-->
        </div>
        <div class="settings">
          <el-button-group class="ml-4">
            <el-button
              type="primary"
              :icon="Search"
              @click="queryShow = !queryShow"
            />
            <el-button type="primary" :icon="RefreshLeft" @click="getList" />
            <el-popover placement="bottom" :width="200" trigger="click">
              <template #reference>
                <el-button type="primary" :icon="Grid" />
              </template>
              <el-checkbox
                v-model="checkAll"
                :indeterminate="isIndeterminate"
                @change="handleCheckAllChange"
                >全选</el-checkbox
              >
              <div class="group-box w-full max-h-[240px] overflow-y-auto">
                <el-checkbox-group
                  v-model="checkedCities"
                  class="columns-show-group"
                  @change="handleCheckedCitiesChange"
                >
                  <el-checkbox
                    v-for="item in defaultColums.data"
                    :key="item.prop"
                    :label="item.prop"
                    >{{ item.label }}</el-checkbox
                  >
                </el-checkbox-group>
              </div>
            </el-popover>
          </el-button-group>
        </div>
      </div>
      <div class="w-full">
        <el-table :data="tableData.data" border width="100%">
          <el-table-column
            v-for="item in colums.data"
            :key="item.id"
            sortable
            :label="item.label"
            :prop="item.key"
            :width="item.width"
            :align="item.align"
          />
          <!--          <el-table-column :fixed="'right'" label="操作" align="center">-->
          <!--            <template #default="scope">-->
          <!--              <el-button text type="primary" @click="del(scope.row)"-->
          <!--                >还原</el-button-->
          <!--              >-->
          <!--              <el-button text type="primary" @click="edit(scope.row)"-->
          <!--                >对比</el-button-->
          <!--              >-->
          <!--            </template>-->
          <!--          </el-table-column>-->
        </el-table>
      </div>
      <!--      <div class="page w-full mt-[10px]">-->
      <!--        <el-pagination-->
      <!--          v-model:current-page="pageObj.data.currentPage"-->
      <!--          v-model:page-size="pageObj.data.pageSize"-->
      <!--          :page-sizes="[10, 20, 30, 40, 50, 100]"-->
      <!--          layout="total, sizes, prev, pager, next, jumper"-->
      <!--          :total="pageObj.data.total"-->
      <!--          @size-change="handleSizeChange"-->
      <!--          @current-change="handleCurrentChange"-->
      <!--        />-->
      <!--      </div>-->
    </el-card>
    <!--    <el-dialog v-model="addDialog" :title="title" width="60%">-->
    <!--      <div class="w-full h-[500px]">-->
    <!--        <v-chart :options="options" />-->
    <!--      </div>-->
    <!--    </el-dialog>-->
  </div>
</template>

<style scoped lang="scss">
.my-card-full {
  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;

    .demo-tabs {
      display: flex;
      flex: 1;
      flex-direction: column;
      overflow: hidden;

      .el-tabs__content {
        flex: 1;

        .el-tab-pane {
          width: 100%;
          height: 100%;
        }
      }
    }
  }
}
</style>
